package com.yiren.dbaa.module.help_center.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;

import java.util.ArrayList;

/**
 * @author lwb
 * @date : 2022/12/9 10:35
 * @description:  触摸测试页面
 */
public class PointerLocationView extends BaseActivity {
    private TextView touchView;
    private TextView eventlabel;
    private int StartX;
    private int StartY;
    private int StopX;
    private int StopY;
    private float historyX;
    private float historyY;
    private int historySize;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_pointer_location_view);
        final int[] event_label_arr = new int[]{R.id.event_label0, R.id.event_label1, R.id.event_label2, R.id.event_label3,
                R.id.event_label4, R.id.event_label5, R.id.event_label6, R.id.event_label7, R.id.event_label8, R.id.event_label9};
        final int[] ColorArr = new int[]{
                Color.rgb(255, 0, 0), Color.rgb(0, 128, 0),
                Color.rgb(0, 139, 139), Color.rgb(0, 0, 255),
                Color.rgb(255, 0, 255), Color.rgb(255, 255, 0),
                Color.rgb(139, 0, 0), Color.rgb(0, 100, 0),
                Color.rgb(0, 0, 139), Color.rgb(0, 255, 0)};
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        final int width = wm.getDefaultDisplay().getWidth();
        final int height = wm.getDefaultDisplay().getHeight();
        final ImageView iv = findViewById(R.id.iv);
        final Bitmap bitmap = null;
        final Bitmap bitmap1 = bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        final SparseArray<Path> mPaths = new SparseArray<>();
        final Paint paint0 = new Paint();
        paint0.setStrokeWidth(3);
        paint0.setStyle(Paint.Style.STROKE);
        paint0.setStrokeCap(Paint.Cap.ROUND);
        paint0.setStrokeJoin(Paint.Join.ROUND);
        paint0.setDither(true);
        paint0.setPathEffect(new CornerPathEffect(2));
        paint0.setColor(ColorArr[0]);
        paint0.setAntiAlias(true);
        paint0.setStrokeMiter(90);
        Paint paint1 = new Paint();
        paint1.setStrokeWidth(3);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeCap(Paint.Cap.ROUND);
        paint1.setStrokeJoin(Paint.Join.ROUND);
        paint1.setDither(true);
        paint1.setPathEffect(new CornerPathEffect(2));
        paint1.setColor(ColorArr[1]);
        paint1.setAntiAlias(true);
        paint1.setStrokeMiter(90);
        Paint paint2 = new Paint();
        paint2.setStrokeWidth(3);
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setStrokeCap(Paint.Cap.ROUND);
        paint2.setStrokeJoin(Paint.Join.ROUND);
        paint2.setDither(true);
        paint2.setPathEffect(new CornerPathEffect(2));
        paint2.setColor(ColorArr[2]);
        paint2.setAntiAlias(true);
        Paint paint3 = new Paint();
        paint3.setStrokeWidth(3);
        paint3.setStyle(Paint.Style.STROKE);
        paint3.setStrokeCap(Paint.Cap.ROUND);
        paint3.setStrokeJoin(Paint.Join.ROUND);
        paint3.setDither(true);
        paint3.setPathEffect(new CornerPathEffect(2));
        paint3.setColor(ColorArr[3]);
        paint3.setAntiAlias(true);
        Paint paint4 = new Paint();
        paint4.setStrokeWidth(3);
        paint4.setStyle(Paint.Style.STROKE);
        paint4.setStrokeCap(Paint.Cap.ROUND);
        paint4.setStrokeJoin(Paint.Join.ROUND);
        paint4.setDither(true);
        paint4.setPathEffect(new CornerPathEffect(2));
        paint4.setColor(ColorArr[4]);
        paint4.setAntiAlias(true);
        Paint paint5 = new Paint();
        paint5.setStrokeWidth(3);
        paint5.setStyle(Paint.Style.STROKE);
        paint5.setStrokeCap(Paint.Cap.ROUND);
        paint5.setStrokeJoin(Paint.Join.ROUND);
        paint5.setDither(true);
        paint5.setPathEffect(new CornerPathEffect(2));
        paint5.setColor(ColorArr[5]);
        paint5.setAntiAlias(true);
        Paint paint6 = new Paint();
        paint6.setStrokeWidth(3);
        paint6.setStyle(Paint.Style.STROKE);
        paint6.setStrokeCap(Paint.Cap.ROUND);
        paint6.setStrokeJoin(Paint.Join.ROUND);
        paint6.setDither(true);
        paint6.setPathEffect(new CornerPathEffect(2));
        paint6.setColor(ColorArr[6]);
        paint6.setAntiAlias(true);
        Paint paint7 = new Paint();
        paint7.setStrokeWidth(3);
        paint7.setStyle(Paint.Style.STROKE);
        paint7.setStrokeCap(Paint.Cap.ROUND);
        paint7.setStrokeJoin(Paint.Join.ROUND);
        paint7.setDither(true);
        paint7.setPathEffect(new CornerPathEffect(2));
        paint7.setColor(ColorArr[7]);
        paint7.setAntiAlias(true);
        paint7.setStrokeMiter(90);
        Paint paint8 = new Paint();
        paint8.setStrokeWidth(3);
        paint8.setStyle(Paint.Style.STROKE);
        paint8.setStrokeCap(Paint.Cap.ROUND);
        paint8.setStrokeJoin(Paint.Join.ROUND);
        paint8.setDither(true);
        paint8.setPathEffect(new CornerPathEffect(2));
        paint8.setColor(ColorArr[8]);
        paint8.setAntiAlias(true);
        Paint paint9 = new Paint();
        paint9.setStrokeWidth(3);
        paint9.setStyle(Paint.Style.STROKE);
        paint9.setStrokeCap(Paint.Cap.ROUND);
        paint9.setStrokeJoin(Paint.Join.ROUND);
        paint9.setDither(true);
        paint9.setPathEffect(new CornerPathEffect(2));
        paint9.setColor(ColorArr[9]);
        paint9.setAntiAlias(true);
        final Paint paint = new Paint();
        paint.setStrokeWidth(4);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setDither(true);
        paint.setPathEffect(new CornerPathEffect(2));
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStrokeMiter(90);
        final ArrayList<Paint> paints = new ArrayList<>();
        paints.add(paint0);
        paints.add(paint1);
        paints.add(paint2);
        paints.add(paint3);
        paints.add(paint4);
        paints.add(paint5);
        paints.add(paint6);
        paints.add(paint7);
        paints.add(paint8);
        paints.add(paint9);
        final Canvas canvas = new Canvas(bitmap1);
        final Path path = new Path();
        touchView = findViewById(R.id.touch_area);
        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getActionMasked();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_POINTER_DOWN:
                        int actionIndex = event.getActionIndex();
                        int PointID = event.getPointerId(actionIndex);
                        for (int i = 0; i < event.getPointerCount(); i++) {
                            int x = (int) event.getX(actionIndex);
                            int y = (int) event.getY(actionIndex);
                            eventlabel = findViewById(event_label_arr[PointID]);
                            String eventType1 = "第" + (PointID + 1) + "个手指头按下了！";
                            String msg1 = "";
                            msg1 += "Action:" + eventType1 + "  ";
                            msg1 += "相对坐标:X:" + String.valueOf(x) + ";Y:" + String.valueOf(y) + "  ";
                            eventlabel.setText(msg1);
                            Path path = new Path();
                            path.moveTo(event.getX(actionIndex), event.getY(actionIndex));
                            mPaths.append(PointID, path);
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        for (int i = 0; i < event.getPointerCount(); i++) {
                            PointID = event.getPointerId(i);
                            int PointIndex = event.findPointerIndex(PointID);
                            String eventType4 = "第" + (PointID + 1) + "个手指在移动";
                            eventlabel = findViewById(event_label_arr[PointID]);
                            int x = (int) event.getX(PointIndex);
                            int y = (int) event.getY(PointIndex);
                            String msg4 = "";
                            msg4 += "Action:" + eventType4 + "  ";
                            msg4 += "相对坐标:" + String.valueOf(x) + "," + String.valueOf(y) + "  ";
                            eventlabel.setText(msg4);
                            historySize = event.getHistorySize();
                            for (int j = 0; j < historySize; j++) {
                                historyX = event.getHistoricalX(PointIndex,j);
                                historyY = event.getHistoricalY(PointIndex,j);
                                Path path = mPaths.get(event.getPointerId(i));
                                path.lineTo(historyX,historyY);
                            }
                            Path path = mPaths.get(event.getPointerId(i));
                            path.lineTo(event.getX(i), event.getY(i));
                        }
                        for (int i = 0; i < mPaths.size(); i++) {
                            canvas.drawPath(mPaths.valueAt(i),paints.get(i));
                        }
                        iv.setImageBitmap(bitmap1);
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        PointID = event.getPointerId(event.getActionIndex());
                        int PointIndex = event.findPointerIndex(PointID);
                        eventlabel = findViewById(event_label_arr[PointID]);
                        int x = (int) event.getX(PointIndex);
                        int y = (int) event.getY(PointIndex);
                        String eventType3 = "#ID=" + (PointID + 1) + "的手指离开了！";
                        String msg3 = "";
                        msg3 += "Action:" + eventType3 + "  ";
                        msg3 += "相对坐标:X:" + String.valueOf(x) + ",Y:" + String.valueOf(y) + "  ";
                        eventlabel.setText(msg3);
                        break;
                    case MotionEvent.ACTION_UP:
                        String msg2 = "手指都离开了";
                        for (int i = 0; i < event_label_arr.length; i++) {
                            eventlabel = findViewById(event_label_arr[i]);
                            eventlabel.setText(msg2);
                        }
                        break;
                }
                return true;
            }
        });
    }

}
